package handler

import (
	"common"
	"common/biz"
	"common/logs"
	"context"
	"core/repo"
	"core/service"
	"encoding/json"
	"framework/remote"
	"hall/logic"
	"hall/models/request"
	"hall/models/response"
	"strconv"
)

type GoodsHandler struct {
	goodsService     *service.GoodsService
	warehouseService *service.WarehouseService
	userService      *service.UserService
	gm               *logic.GoodsManager
	base             *BaseHandler
}

func (h *GoodsHandler) List(session *remote.Session, msg []byte) any {
	logs.Info("[Goods] List msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[Goods] List err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.GoodsIDAsPage
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[Goods]用户%d List err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	// 判断用户是否进入商城，没有进入就添加一个用户
	_, err := h.gm.GetGoodsUser(uint(uid), h.userService)
	if err != nil {
		return common.F(err)
	}
	count, isIOS, list := h.goodsService.GetList(context.TODO(), uint(uid), &req)
	return common.S(response.GoodsListMsg{
		Count: count,
		IsIos: isIOS,
		List:  list,
	})
}

func (h *GoodsHandler) Order(session *remote.Session, msg []byte) any {
	logs.Info("[Goods] Order msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[Goods] Order err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.CommonID
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[Goods]用户%d Order err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	userData, err := h.gm.GetGoodsUser(uint(uid), h.userService)
	if err != nil {
		return common.F(err)
	}
	// 数据检验
	err = h.base.Check(userData)
	if err != nil {
		return common.F(err)
	}
	order, err := h.gm.CreateOrder(uint(uid), req.ID, session)
	if err != nil {
		return common.F(err)
	}
	return common.S(order)
}

func (h *GoodsHandler) Pay(session *remote.Session, msg []byte) any {
	logs.Info("[Goods] Pay msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[Goods] Pay err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.GoodsPay
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[Goods]用户%d Pay err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	userData, err := h.gm.GetGoodsUser(uint(uid), h.userService)
	if err != nil {
		return common.F(err)
	}
	// 数据检验
	err = h.base.Check(userData)
	if err != nil {
		return common.F(err)
	}
	payUrl, err := h.gm.OrderPay(uint(uid), req.OrderNo, req.Method)
	if err != nil {
		return common.F(err)
	}
	return common.S(map[string]any{
		"payUrl": payUrl,
	})
}

func (h *GoodsHandler) OrderList(session *remote.Session, msg []byte) any {
	logs.Info("[Goods] OrderList msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[Goods] OrderList err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.GoodsIDAsPage
	req.Type = 2
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[Goods]用户%d Pay err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	// 判断用户是否进入商城，没有进入就添加一个用户
	_, err := h.gm.GetGoodsUser(uint(uid), h.userService)
	if err != nil {
		return common.F(err)
	}
	count, list := h.goodsService.GetOrderList(context.TODO(), uint(uid), &req)
	return common.S(map[string]any{
		"count": count,
		"list":  list,
	})
}

// PayPayment 苹果支付
func (h *GoodsHandler) PayPayment(session *remote.Session, msg []byte) any {
	logs.Info("[Goods] PayPayment msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[Goods] PayPayment err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.GoodsOrderNo
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[Goods]用户%d PayPayment err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	err := h.goodsService.IosPayOrder(ctx, uint(uid), req.OrderNo)
	if err != nil {
		return common.F(err)
	}
	return common.S("success")
}

func NewGoodsHandler(r *repo.Manager, gm *logic.GoodsManager) *GoodsHandler {
	return &GoodsHandler{
		goodsService:     service.NewGoodsService(r),
		warehouseService: service.NewWarehouseService(r),
		userService:      service.NewUserService(r),
		gm:               gm,
		base:             NewBaseHandler(r),
	}
}
